package jp.co.systemi.asp.workflow.cron;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.systemi.asp.workflow.data.PaidHoliday;
import jp.co.systemi.asp.workflow.data.User;
import jp.co.systemi.asp.workflow.mail.Mail;
import jp.co.systemi.asp.workflow.peer.PaidHolidayPeer;
import jp.co.systemi.asp.workflow.peer.UserPeer;

import org.datanucleus.util.StringUtils;

public class MonthlyReport extends HttpServlet {
    private static final Logger log = Logger.getLogger(MonthlyReport.class
            .getName());
    private static final long serialVersionUID = -1574055166182829807L;
    private static ResourceBundle resourceBundle = ResourceBundle
            .getBundle(MonthlyReport.class.getName().replace(".", "/"));
    private static ResourceBundle mailBundle = ResourceBundle
        .getBundle(Mail.class.getName().replace(".", "/"));
    static SimpleDateFormat sdf = new SimpleDateFormat(resourceBundle
            .getString("date.format"));

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String month = request.getParameter("month");
        Calendar prevMonthCal = Calendar.getInstance();
        if (!StringUtils.isEmpty(month)) {
            // 画面からのリクエスト
            if (!month.matches("(\\d{4,4})(\\d{2,2})")) {
                log.info("パラメータエラー");
                return;
            }
            prevMonthCal.set(Calendar.YEAR, Integer.parseInt(month.substring(
                    0, 4)));
            prevMonthCal.set(Calendar.MONTH, Integer.parseInt(month.substring(
                    4, 6)) - 1);
        } else {
            // 自動の場合は
            // このタスクは毎月の1日に動くとして、前月の休暇状況を取得する
            prevMonthCal.add(Calendar.DATE, -1);
        }
        List<PaidHoliday> holidays = PaidHolidayPeer
                .getPaidHolidayData(prevMonthCal.getTime());
        // メール送信
        Mail mail = new Mail(this.getClass().getPackage().getName().replace(
                ".", "/")
                + "/MonthlyReportPaidHoliday.vm");
        // Fromを設定
        User mailSender = new User();
        mailSender.setLoginId(mailBundle.getString("from"));
        mailSender.setFirstName(mailBundle.getString("title"));

//        User admin = UserPeer.getAdmin();
        mail.setFrom(mailSender.getLoginId(), UserPeer.getName(mailSender));
        // 総務へ送信
        User generalDirector = UserPeer
                .getTheDirectorInChargeOfGeneralAffairs();
        mail.addTo(generalDirector.getLoginId(), UserPeer
                .getName(generalDirector));
        // 件名
        mail.setSubject(sdf.format(prevMonthCal.getTime()) + " "
                + resourceBundle.getString("paidholiday.mail.subject"));

        mail.put("holidays", holidays);
        mail.send();

    }

}
